<template>
	<view class="uni-flex uni-row sensors" v-if="sensors.length">
		<template v-for="sensor in sensors">
			<view class="uni-flex uni-column sensor">
				<text class="name">{{sensor | filterSensorName}}</text>
				<view class="param">
					<text class="value">{{ sensor.value | filterSensorValue}}</text>
					<text class="unit">{{ sensor.unit | filterSensorUnit}}</text>
				</view>
			</view>
		</template>
		<template v-for="valve in getEmptyNumber(sensors)">
			<view class="empty"></view>
		</template>
	</view>
</template>

<script>
	export default{
		data(){
			return {}
		},
		props:{
			sensors: {
				type: Array,
				default: () => [],
				validator(value) {
					let result = true;
					if(value instanceof Array){
						for(let i = 0; i < value.length; i++){
							let obj = value[i];
							if(obj instanceof Object){
								result &= typeof obj.index === 'number';
								result &= typeof obj.name === 'string';
								result &= typeof obj.value === 'number';
								result &= typeof obj.unit === 'string';
								
								if(!result) return false;
							} else {
								return false;
							}
						}
					}
					return result;
				}
			}
		},
		methods:{
			getEmptyNumber(sensors){
				let num = sensors.length%4
				return num ? 4-num : num;
			},
		},
		filters:{
			filterSensorName(sensor){
				return sensor.name
			},
			filterSensorValue(value){
				return value === 0x7fff ? "------" : value
			},
			filterSensorUnit(unit){
				return !!unit ? unit : ""
			}
		}
	}
</script>

<style lang="scss" scoped>
.sensors{
	flex-wrap: wrap;
	.sensor, .empty{
		// width: 20%;
		flex: 23%;
		height: 117.64rpx;
		text-align: center;
		border-radius: 11.76rpx;
		border: 1.17rpx solid #FFFFFF;
		background: #7DB6A8;
		justify-content: center;
		align-items: center;
		.name{
			font-size: 28.23rpx;
			line-height: 28.23rpx;
			margin-bottom: 11.76rpx;
		}
		.param{
			.value{
				letter-spacing: 0.1em;
			}
			.unit{}
		}
	}
	.empty{
		background: transparent;
	}
}
</style>
